package cdc.retrofit.official.chunking;
import java.io.IOException;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.Retrofit;
public class ChunkingConverter {
	 public static void main(String... args) throws IOException, InterruptedException {
		    MockWebServer server = new MockWebServer();
		    server.enqueue(new MockResponse());
		    server.enqueue(new MockResponse());
		    server.start();

		    Retrofit retrofit = new Retrofit.Builder()
		        .baseUrl(server.url("/"))
		        .addConverterFactory(new ChunkingConverterFactory())
		        .addConverterFactory(GsonConverterFactory.create())
		        .build();
		    Service service = retrofit.create(Service.class);

		    Repo retrofitRepo = new Repo("square", "retrofit");

		    service.sendNormal(retrofitRepo).execute();
		    RecordedRequest normalRequest = server.takeRequest();
		    System.out.println(
		        "Normal @Body Transfer-Encoding: " + normalRequest.getHeader("Transfer-Encoding"));

		    service.sendChunked(retrofitRepo).execute();
		    RecordedRequest chunkedRequest = server.takeRequest();
		    System.out.println(
		        "@Chunked @Body Transfer-Encoding: " + chunkedRequest.getHeader("Transfer-Encoding"));

		    server.shutdown();
		    
		    server.close();
		  }

}
